package us.icitap.commandcenter.ui.operator;


import java.util.List;
import java.util.logging.Level;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.Result;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.listeners.NewCallListener;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.utils.HandleNotification;

import com.vaadin.event.Action;

public class OperatorVerticalLayout extends CustVerticalLayout implements NewCallListener{


	final CallForm callForm = new CallForm(this);
	final CallList callList = new CallList();
	 
	static final Action ADD_CALL =  new Action("Shto telefonate");
	 
	public OperatorVerticalLayout() {
		
		this.addComponent(this.callForm);
		
		Result<List<Call>> result = CommandCenterUI.appStatelessBean.getOperatorCallList(CommandCenterUI.getUser().getUsername());
		if(result.isSucceeded())
			this.callList.addCallToTable(result.getObject());
		else
			HandleNotification.showErrorOnScreen(result.getMessage());
		
		this.addComponent(this.callList);
		this.setMargin(true);
		this.setSpacing(true);
		
		callList.addActionHandler(new Action.Handler() {
			
			@Override
			public void handleAction(Action action, Object sender, Object target) {
				Call call = (Call) target;
				callForm.resetForm(call);
			}
			@Override
			public Action[] getActions(Object target, Object sender) {
				 if(target == null) return null;
	               return new Action[]{ADD_CALL};
			}
		});
	}

	@Override
	public void addNewCall(Call call) {
		Result<Call> result = CommandCenterUI.appStatelessBean.addNewCall(call, true);
		if(result.isSucceeded()){
			callList.addNewCall(result.getObject());
			callForm.resetForm();	
		}
		else
			CommandCenterUI.LOG.logp(Level.SEVERE, "CallForm", "actYes", result.getMessage());
		
		
	}
}
